package cn.itcast.config;

import cn.itcast.factory.TransactionManagementFactory;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.mapper.MapperScannerConfigurer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.*;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.sql.DataSource;

@Configuration
@ComponentScan(value = "cn.itcast", excludeFilters
        = @ComponentScan.Filter(type = FilterType.ANNOTATION,classes = Controller.class) )
//开启注解aop支持
@EnableAspectJAutoProxy
//开启注解事务控制的支持
@EnableTransactionManagement
@Import({TransactionManagementFactory.class})
public class SpringConfig {

    //设置sqlsessionFactorybean
    @Bean
    public SqlSessionFactory creatSqlSessionFactory(@Autowired DataSource dataSource) throws Exception {
        SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
        sqlSessionFactoryBean.setDataSource(dataSource);
        return sqlSessionFactoryBean.getObject();
    }
    @Bean
    public MapperScannerConfigurer creatMapperScannerConfigurer(){
        MapperScannerConfigurer mapper = new MapperScannerConfigurer();
        //加载mapper包中的UserMapper类为其提供实现类
        mapper.setBasePackage("cn.itcast.mapper");
        return mapper;
    }
}
